/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jhu.profiteer.data;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import jhu.profiteer.data.exceptions.PreexistingEntityException;

/**
 *
 * @author Jeremy
 */
@Entity
public class Location implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private String name;
  private String description;
  private Long x;
  private Long y;

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  @Override
  public int hashCode() {
    int hash = 0;
    hash += (null != id ? id.hashCode() : 0) +
            (null != name ? name.hashCode() : 0) +
            (null != description ? description.hashCode() : 0) +
            (null != x ? x.hashCode() : 0) +
            (null != y ? y.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object object) {
    if (!(object instanceof Location)) {
      return false;
    }
    Location other = (Location) object;
    if ((null == this.id && null != other.id) ||
        (null != this.id && !this.id.equals(other.id))) {
      return false;
    }
    if ((null == this.name && null != other.name) ||
        (null != this.name && !this.name.equals(other.name))) {
      return false;
    }
    if ((null == this.description && null != other.description) ||
        (null != this.description &&
         !this.description.equals(other.description))) {
      return false;
    }
    if ((null == this.x && null != other.x) ||
        (null != this.x && !this.x.equals(other.x))) {
      return false;
    }
    if ((null == this.y && null != other.y) ||
        (null != this.y && !this.y.equals(other.y))) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return "jhu.profiteer.data.Location[id=" + id +
           ", name=" + name +
           ", description=" + description +
           ", x=" + x +
           ", y=" + y + "]";
  }

  /**
   * @return the name
   */
  public String getName() {
    return name;
  }

  /**
   * @param name the name to set
   */
  public void setName(String name) {
    this.name = name;
  }

  /**
   * @return the description
   */
  public String getDescription() {
    return description;
  }

  /**
   * @param description the description to set
   */
  public void setDescription(String description) {
    this.description = description;
  }

  /**
   * @return the x
   */
  public Long getX() {
    return x;
  }

  /**
   * @param x the x to set
   */
  public void setX(Long x) {
    this.x = x;
  }

  /**
   * @return the y
   */
  public Long getY() {
    return y;
  }

  /**
   * @param y the y to set
   */
  public void setY(Long y) {
    this.y = y;
  }

  public Long fuelCost(Location l)
  {
    if(null == l) return null;
    if(this.id == l.getId()) return 0L;
    Long xDiff = this.x - l.getX();
    Long x2 = xDiff * xDiff;
    Long yDiff = this.y - l.getY();
    Long y2 = yDiff * yDiff;
    Long dist2 = x2 + y2;
    return Math.round(Math.sqrt(dist2.doubleValue()));
  }

  public boolean canTravel(Airplane a, Location l)
  {
    if(null == a || null == l) return false;
    if(this.id == l.getId()) return false;
    Long dist = this.fuelCost(l);
    if(a.getCurrentFuel() < dist) return false;
    return true;
  }

  public boolean canTravel(Character c, Location l)
  {
    if(null == c) return false;
    AirplaneJpaController jc = new AirplaneJpaController();
    Airplane a = jc.findAirplaneByCharacterId(c.getId());
    return this.canTravel(a, l);
  }

  public void generateCurrentValues(Character c)
  {
    if(null == c) return;
    new CurrentCommodityValueJpaController().destroyWithCharacterId(c.getId());
    List<LocationCommodity> lcs =
            new LocationCommodityJpaController()
            .findLocationCommodityEntitiesWithLocationId(this.id);

    for(LocationCommodity lc : lcs)
    {
      try {
        lc.generateCurrentValue(c);
      } catch (PreexistingEntityException ex) {
        continue;
      } catch (Exception ex) {
        continue;
      }
    }
  }
}